/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.spring.manager.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.genia.toolbox.basics.bean.Transformer;
import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.basics.process.ProxyHelper;
import com.genia.toolbox.persistence.spring.dependency.PersistableDependency;
import com.genia.toolbox.persistence.spring.manager.DependencyManager;

/**
 * this class handles dependencies between persistent object. It registers all
 * <code>PersistableDependency</code> declared in the spring configuration
 * files and then distribute the onCreate/onUpdate/onDelete signal depending on
 * the object being modified.
 */
public class DependencyManagerImpl
    implements DependencyManager
{

  /**
   * a {@link Transformer} to generate list of {@link PersistableDependency}s.
   */
  private final class PersistableDependencyListCreator
      implements Transformer<Class<?>, List<PersistableDependency<?>>>
  {
    /**
     * the transformation method.
     * 
     * @param type
     *          the object to convert
     * @return the converted object
     */
    public List<PersistableDependency<?>> transform(Class<?> type)
    {
      ArrayList<PersistableDependency<?>> res = new ArrayList<PersistableDependency<?>>();
      for (PersistableDependency<?> persistableDependency : persistableDependencies) {
        if (persistableDependency.getType().isAssignableFrom(type)) {
          res.add(persistableDependency);
        }
      }
      return res;
    }
  }

  /**
   * the map of all <code>PersistableDependency</code> indexed by the
   * <code>Class</code> of objects they serve.
   */
  private final Map<Class<?>, List<PersistableDependency<?>>> dependencies = ProxyHelper.getSafeMap(new ConcurrentHashMap<Class<?>, List<PersistableDependency<?>>>(), new PersistableDependencyListCreator());

  /**
   * the list of {@link PersistableDependency} to register.
   */
  private List<PersistableDependency<?>> persistableDependencies;



  /**
   * handle the dependencies before creating an object.
   * 
   * @param object
   *          the object that will be created
   * @throws BundledException
   *           when an error occurs
   * @see com.genia.toolbox.persistence.spring.manager.DependencyManager#onCreate(com.genia.toolbox.persistence.bean.Persistable)
   */
  @SuppressWarnings("unchecked")
  public void onCreate(final Object object)
      throws BundledException
  {
    for (final PersistableDependency dependency : getPersistableDependencies(object.getClass())) {
      dependency.onCreate(object);
    }
  }



  /**
   * handle the dependencies after creating an object.
   * 
   * @param object
   *          the object that has been created
   * @throws BundledException
   *           when an error occurs
   * @see com.genia.toolbox.persistence.spring.manager.DependencyManager#onPostCreate(com.genia.toolbox.persistence.bean.Persistable)
   */
  @SuppressWarnings("unchecked")
  public void onPostCreate(final Object object)
      throws BundledException
  {
    for (final PersistableDependency dependency : getPersistableDependencies(object.getClass())) {
      dependency.onPostCreate(object);
    }
  }



  /**
   * handle the dependencies after updating an object.
   * 
   * @param object
   *          the object that has been created
   * @throws BundledException
   *           when an error occurs
   * @see com.genia.toolbox.persistence.spring.manager.DependencyManager#onPostUpdate(com.genia.toolbox.persistence.bean.Persistable)
   */
  @SuppressWarnings("unchecked")
  public void onPostUpdate(final Object object)
      throws BundledException
  {
    for (final PersistableDependency dependency : getPersistableDependencies(object.getClass())) {
      dependency.onPostUpdate(object);
    }
  }



  /**
   * handle the dependencies before deleting an object.
   * 
   * @param object
   *          the object that will be deleted
   * @throws BundledException
   *           when an error occurs
   * @see com.genia.toolbox.persistence.spring.manager.DependencyManager#onDelete(com.genia.toolbox.persistence.bean.Persistable)
   */
  @SuppressWarnings("unchecked")
  public void onDelete(final Object object)
      throws BundledException
  {
    for (final PersistableDependency dependency : getPersistableDependencies(object.getClass())) {
      dependency.onDelete(object);
    }
  }



  /**
   * handle the dependencies before updating an object.
   * 
   * @param object
   *          the object that will be updated
   * @throws BundledException
   *           when an error occurs
   * @see com.genia.toolbox.persistence.spring.manager.DependencyManager#onUpdate(com.genia.toolbox.persistence.bean.Persistable)
   */
  @SuppressWarnings("unchecked")
  public void onUpdate(final Object object)
      throws BundledException
  {
    for (final PersistableDependency dependency : getPersistableDependencies(object.getClass())) {
      dependency.onUpdate(object);
    }
  }



  /**
   * this method retrieve all the <code>PersistableDependency</code> that
   * handle a particular type.
   * 
   * @param type
   *          the <code>Class</code> of the object that we search the
   *          dependencies
   * @return the set of all <code>PersistableDependency</code> that handle a
   *         particular type
   */
  private List<PersistableDependency<?>> getPersistableDependencies(final Class<?> type)
  {
    return dependencies.get(type);
  }



  /**
   * getter for the persistableDependencies property.
   * 
   * @return the persistableDependencies
   */
  public List<PersistableDependency<?>> getPersistableDependencies()
  {
    return persistableDependencies;
  }



  /**
   * setter for the persistableDependencies property.
   * 
   * @param persistableDependencies
   *          the persistableDependencies to set
   */
  public void setPersistableDependencies(List<PersistableDependency<?>> persistableDependencies)
  {
    this.persistableDependencies = persistableDependencies;
  }
}
